package com.yf1706.service;

import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import java.util.Set;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yf1706.bean.ExamPaper;
import com.yf1706.bean.ExamPaperQuestion;
import com.yf1706.bean.GapFilling;
import com.yf1706.bean.Judge;
import com.yf1706.bean.Multiselect;
import com.yf1706.bean.SingleChoice;
import com.yf1706.dao.jpa.ExamPaperJDAO;
import com.yf1706.dao.jpa.ExamPaperQuestionJDAO;
import com.yf1706.dao.jpa.fillingJDAO;
import com.yf1706.dao.jpa.judgeJDAO;
import com.yf1706.dao.jpa.multiSelectJDAO;
import com.yf1706.dao.jpa.singleselectJDAO;
import com.yf1706.dao.mybatis.ExamPaperMDAO;
import com.yf1706.dao.mybatis.GapFillingMDAO;
import com.yf1706.dao.mybatis.ISingleChoiceMDAO;
import com.yf1706.dao.mybatis.JudgeMDAO;
import com.yf1706.dao.mybatis.multiselectMDAO;




@Service
@Transactional
public class ExamPaperService {
	@Autowired
	private ExamPaperJDAO epjdao;
	
	@Autowired
	private ExamPaperMDAO epmdao;
	
	@Autowired
	private singleselectJDAO sjdao;
	
	@Autowired
	private multiSelectJDAO mjdao;
	
	@Autowired
	private judgeJDAO jjdao;
	
	@Autowired
	private fillingJDAO fjdao;
	
	@Autowired 
	private ExamPaperQuestionJDAO epqjdao;
	
	@Autowired
	private multiselectMDAO mmdao;
	
	@Autowired
	private ISingleChoiceMDAO smdao;
	
	@Autowired
	private GapFillingMDAO fmdao;
	
	@Autowired
	private JudgeMDAO jmdao;
	

	public void deleteByExamPaperId(int examPaperId) {
		
		epjdao.deleteById(examPaperId);
	}

	public void updataNameById(int examPaperId, String examP) {
		epmdao.updataNameById(examPaperId,examP);
		
	}

	public List<ExamPaper> queryAllExamPaper() {
		
		return epjdao.findAll();
	}

public List<ExamPaper> queryAllExamPaper1() {
		
		return epmdao.queryAllExamPaper();
	}

public void addTM(int qbId, int examPaperId, String examPaperSnum, String examPaperMnum, String examPaperJnum,
		String examPaperFnum) {
	Random ran=new Random();
	List<SingleChoice> list1=smdao.querybyQbId(qbId);
	List<Multiselect> list2=mmdao.querybyQbId(qbId);
	List<GapFilling> list3=fmdao.querybyQbId(qbId);
	List<Judge> list4=jmdao.querybyQbId(qbId); 
	Set<SingleChoice> set=new HashSet<>(list1);
	Set<Multiselect> set2=new HashSet<>(list2);
	Set<GapFilling> set3=new HashSet<>(list3);
	Set<Judge> set4=new HashSet<>(list4);
	while(set.size()<Integer.parseInt(examPaperSnum)) {
		set.add(list1.get(ran.nextInt(list1.size())));
	}
	while(set2.size()<Integer.parseInt(examPaperMnum)) {
		set2.add(list2.get(ran.nextInt(list2.size())));
	}
	while(set3.size()<Integer.parseInt(examPaperFnum)) {
		set3.add(list3.get(ran.nextInt(list3.size())));
	}
	while(set4.size()<Integer.parseInt(examPaperJnum)) {
		set4.add(list4.get(ran.nextInt(list4.size())));
	}
	Iterator<SingleChoice> iterator=set.iterator();
	while(iterator.hasNext()){
		ExamPaperQuestion eq=new ExamPaperQuestion();
		eq.setSingleChoice(sjdao.findBySingleChoiceId(iterator.next().getSingleChoiceId()).get(0));
		eq.setExamPaper(epjdao.findByExamPaperId(examPaperId).get(0));
		eq.setQuestionType("单选");
		epqjdao.save(eq);
	}
	Iterator<Multiselect> iterator2=set2.iterator();
	while(iterator2.hasNext()){
		ExamPaperQuestion eq=new ExamPaperQuestion();
		eq.setMultiselect(mjdao.findByMultiselectId(iterator2.next().getMultiselectId()).get(0));
		eq.setExamPaper(epjdao.findByExamPaperId(examPaperId).get(0));
		eq.setQuestionType("多选");
		epqjdao.save(eq);
	}
	
	Iterator<GapFilling> iterator3=set3.iterator();
	while(iterator3.hasNext()){
		ExamPaperQuestion eq=new ExamPaperQuestion();
		eq.setGapFilling(fjdao.findByGapFillingId(iterator3.next().getGapFillingId()).get(0));
		eq.setExamPaper(epjdao.findByExamPaperId(examPaperId).get(0));
		eq.setQuestionType("填空");
		epqjdao.save(eq);
	}
	
	Iterator<Judge> iterator4=set4.iterator();
	while(iterator4.hasNext()){
		ExamPaperQuestion eq=new ExamPaperQuestion();
		eq.setJudge(jjdao.findByJudgeId(iterator4.next().getJudgeId()).get(0));
		eq.setExamPaper(epjdao.findByExamPaperId(examPaperId).get(0));
		eq.setQuestionType("填空");
		epqjdao.save(eq);
	}
}

}
